import { defineStore } from 'pinia'
import { getUserInfo, getMyAppointment, startAppointment, cancelAppointment } from '../api/userInfo'
import { computed, ref } from 'vue'
export const useUserInfoStore = defineStore('userInfo', () => {
    let myInfo = ref({})
    let myReservation = ref({})
    // 获取用户信息
    const getUserInfoData = async () => {
        try {
            const res = await getUserInfo()
            myInfo.value = res.data
        }
        catch (err) {
            console.log(err);
        }
    }
    //获取预约数据
    const getMyReservationData = async () => {
        try {
            const res = await getMyAppointment()
            myReservation.value = res.data
        }
        catch (err) {
            console.log(err);
        }
    }
    //取消预约
    const cancelReservation = async (data) => {
        try {
            const res = await cancelAppointment(data)
            return res
        }
        catch (err) {
            console.log(err);
        }
    }
    //发送预约请求
    const sendReservation = async (data) => {
        try {
            const res = await startAppointment(data)
            return res
        }
        catch (err) {
            console.log(err);
        }
    }
    const reservationStatus = computed(() => {
        if (!myReservation.value || typeof myReservation.value.reservation_status === 'undefined') {
            return '未获取到预约状态';
        }
        else if (myReservation.value.reservation_status === 4) {
            return '已过期'
        }
        else if (myReservation.value.reservation_status === 1) {
            return '申请中...'
        }
        else if (myReservation.value.reservation_status === 2) {
            return '已拒绝'
        }
        else if (myReservation.value.reservation_status === 3) {
            return '已接受'
        }
        else {
            return '未申请'
        }
    }, {
        deep: true
    })

    return { myInfo, myReservation, reservationStatus, sendReservation, cancelReservation, getUserInfoData, getMyReservationData }
})